package com.copa.l1813;

/**
 * @author copa
 * @createDate 2023-01-16 11:06
 * @function 双指针法
 */
public class Test1 {

    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        String[] s1Word = sentence1.split(" ");
        String[] s2Word = sentence2.split(" ");
        // 标记左边相同单词数
        int left = 0;
        // 标记右边相同单词数
        int right = 0;
        while (left < s1Word.length
                && left < s2Word.length
                && s1Word[left].equals(s2Word[left])) {
            left++;
        }
        while (right < s1Word.length - left
                && right < s2Word.length - left
                && s1Word[s1Word.length - right - 1].equals(s2Word[s2Word.length - right - 1])) {
            right++;
        }
        // 如果标记左边相同单词数 + 标记右边相同单词数 与 两字符串单词（最短那个）数量相等，即认为相似
        return left + right == Math.min(s1Word.length, s2Word.length);
    }

    public static void main(String[] args) {
//        String sentence1 = "My name is Haley";
//        String sentence2 = "My Haley";
        String sentence1 = "a";
        String sentence2 = "a lot of piece";
        System.out.println(new Test1().areSentencesSimilar(sentence1, sentence2));
    }
}
